package com.clannad.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clannad.commonutils.R;
import com.clannad.eduservice.entity.EduCourse;
import com.clannad.eduservice.entity.vo.CourseInfoVo;
import com.clannad.eduservice.entity.vo.CoursePublishVo;
import com.clannad.eduservice.service.EduCourseService;
import com.sun.org.apache.bcel.internal.generic.LADD;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author clannad
 * @since 2022-08-11
 */
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin
@Api(tags = "课程管理")
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;

    @PostMapping("addCourseInfo")
    @ApiOperation("添加课程")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String id=eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    @GetMapping("getCourseInfo/{courseId}")
    @ApiOperation("查出课程信息")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo=eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    @PostMapping("updateCourseInfo")
    @ApiOperation("修改课程信息")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @GetMapping("getPublishCourseInfo/{id}")
    @ApiOperation("最终课程确认查询")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo=eduCourseService.getPublishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    @PostMapping("publishCourse/{id}")
    @ApiOperation("最终发布")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    @PostMapping("/{pageNum}/{limit}")
    @ApiOperation("多条件查询课程信息")
    public R getCourseList(@RequestBody(required = false) EduCourse eduCourse,@PathVariable long pageNum,@PathVariable long limit){
        Page<EduCourse> page=new Page(pageNum,limit);
        LambdaQueryWrapper<EduCourse> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        String title = eduCourse.getTitle();
        String status = eduCourse.getStatus();
        if(!StringUtils.isEmpty(title)){
            lambdaQueryWrapper.like(EduCourse::getTitle,title);
        }
        if(!StringUtils.isEmpty(status)){
            lambdaQueryWrapper.eq(EduCourse::getStatus,status);
        }
        lambdaQueryWrapper.orderByDesc(EduCourse::getGmtCreate);
        eduCourseService.page(page,lambdaQueryWrapper);
        List<EduCourse> records = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("list",records).data("total",total);
    }

    @DeleteMapping("/{courseId}")
    @ApiOperation("删除课程")
    public R deleteCourse(@PathVariable String courseId){
        eduCourseService.removeCourse(courseId);
        return R.ok();
    }
}

